﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Xml.Linq;
using SGP;
namespace Animation_tools
{
    public class Animation
    {
      public class Frame
        {
            public class rect
            {
                int right;

                public int Right
                {
                    get { return right; }
                    set { right = value; }
                }
                int left;

                public int Left
                {
                    get { return left; }
                    set { left = value; }
                }
                int bottom;

                public int Bottom
                {
                    get { return bottom; }
                    set { bottom = value; }
                }
                int top;

                public int Top
                {
                    get { return top; }
                    set { top = value; }
                }
            };
            public override string ToString()
            {
                return trigger;
            }
            public Point Anchorpoint;
            public rect SrcRect;
            //public rect CollisionRect;
            //public rect DmgRect;

            public double fDuration;
           // public int nDamage;

            //something about an event/trigger here
            public string trigger;
            public Frame()
            {
            SrcRect = new rect();
            fDuration = 0.0f;
            trigger = "";
            }
        };

       public class Animationinfo
        {
            public Animation CurAnimation;
            public int CurrFrame;
            public float TimeWaited;
            public Animationinfo()
            {
                CurrFrame = 0;
            }
        };

       public Animationinfo animation;
       public List<Frame> Frames;
        public int TextureID;
        public string rst;
        bool IsLooping;
       public bool Playing;
        float PlaybackSpeed;
        bool IsPlaying()
        {
            return Playing;
        }
        public Animation()
        {
            animation = new Animationinfo();
            animation.CurAnimation = this;
            TextureID = -1;
            IsLooping = true;
            Playing = true;
            PlaybackSpeed = 0.5f;
            Frames = new List<Frame>();
        }
        public bool Initialize(string szFilename)
        {
            XElement Root = XElement.Load(szFilename);
            if (Root == null)
                return false;

            string imgname = "resource/";
            rst = Root.Attribute("Name").Value;
            string img = imgname + rst;
            TextureID = CSGP_TextureManager.GetInstance().LoadTexture(img);
            XElement anime = Root.Element("Frames");
            IEnumerable<XElement> frames = anime.Elements();
            animation.CurrFrame = 0;
            foreach(XElement fram in frames)
            {
                Frame info = new Frame();
                info.trigger = fram.Attribute("Trigger").Value;
                info.fDuration = Convert.ToDouble(fram.Attribute("Duration").Value);
                info.Anchorpoint.Y = Convert.ToInt32(fram.Attribute("Y").Value);
                info.Anchorpoint.X = Convert.ToInt32(fram.Attribute("X").Value);
                info.SrcRect.Right = Convert.ToInt32(fram.Attribute("Right").Value);
                info.SrcRect.Left = Convert.ToInt32(fram.Attribute("Left").Value);
                info.SrcRect.Bottom = Convert.ToInt32(fram.Attribute("Bottom").Value);
                info.SrcRect.Top = Convert.ToInt32(fram.Attribute("Top").Value);
                Frames.Add(info);
                frames = fram.ElementsAfterSelf("Frame");
            }
            return true;
        }
        public void Update(float dt)
        {
            if (Playing == false)
                return;

            animation.TimeWaited += dt * PlaybackSpeed;
            if (animation.TimeWaited > Frames[animation.CurrFrame].fDuration)
            {
                animation.TimeWaited = 0.0f;
                ++animation.CurrFrame;

                // Is this the end?
                if (IsOver() == true)
                {
                    if (IsLooping == true)
                        animation.CurrFrame = 0;
                    else
                    {
                        // Stop on the last valid frame
                        Playing = false;
                        --animation.CurrFrame;
                    }
                }
            }
        }

        bool IsOver()
        {
            if (animation.CurrFrame == Frames.Count)
            {
                return true;
            }
            else
                return false;
        }

        public void Render(int X, int Y, bool Flipped, float Scale, Color dwColor)
        {
            float ScaleX = Scale;
            Rectangle rframe = new Rectangle();
            rframe.Height = Frames[animation.CurrFrame].SrcRect.Bottom - Frames[animation.CurrFrame].SrcRect.Top;
            rframe.Width = Frames[animation.CurrFrame].SrcRect.Right - Frames[animation.CurrFrame].SrcRect.Left;
            if (Flipped == true)
                ScaleX = -ScaleX; // flipping the image

            CSGP_TextureManager.GetInstance().Draw(TextureID, (int)(X - (Frames[animation.CurrFrame].Anchorpoint.X * ScaleX)), (int)(Y - (Frames[animation.CurrFrame].Anchorpoint.Y * Scale)), ScaleX, Scale, rframe, 0.0f, 0.0f, 0.0f, dwColor);
        }
    }
}
